<?php
/*  
 *  Project lonely
 *  Author: Fvcity dev Team
 *  File: /lonely/functions.php
 *
 *  Subversion Keywords:
 *  $Id: functions.php 19 2007-07-05 05:21:52Z fvcity.roy $
 *  $Date: 2007-07-05 05:21:52 +0000 (Thu, 05 Jul 2007) $
 *  $Revision: 19 $
 *  $Author: fvcity.roy $
 *  $URL: http://fvcity.googlecode.com/svn/trunk/functions.php $
 */

/* Widget Settings */

if ( function_exists('register_sidebar') )
	register_sidebar(array(
		'name' => 'Sidebar',
		'before_widget' => '<div class="Categories">', 
		'after_widget' => '</div>', 
		'before_title' => '<h3>',
		'after_title' => '</h3>',
	));
/* end Widget Settings */

/* 分页 */
function fvcity_pagenavi($before = '', $after = '', $prelabel = '', $nxtlabel = '', $pages_to_show = 5, $always_show = false) {
	global $request, $posts_per_page, $wpdb, $paged;
	if(empty($prelabel)) {
		$prelabel  = '&laquo;';
	}
	if(empty($nxtlabel)) {
		$nxtlabel = '&raquo;';
	}
	$half_pages_to_show = round($pages_to_show/2);
	if (!is_single()) {
		if(!is_category()) {
			preg_match('#FROM\s(.*)\sORDER BY#siU', $request, $matches);
		} else {
			preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
		}
		$fromwhere = $matches[1];
		$numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
		$max_page = ceil($numposts /$posts_per_page);
		if(empty($paged)) {
			$paged = 1;
		}
		if($max_page > 1 || $always_show) {
			echo "$before <span class=\"fvpage\">Pages ($max_page):</span> <strong>";
			if ($paged >= ($pages_to_show-1)) {
				echo '<a href="'.get_pagenum_link().'">&laquo; First</a> <span class="fvpage">...</span> ';
			}
			previous_posts_link($prelabel);
			for($i = $paged - $half_pages_to_show; $i  <= $paged + $half_pages_to_show; $i++) {
				if ($i >= 1 && $i <= $max_page) {
					if($i == $paged) {
						echo "<span class=\"fvpage\">$i</span>";
					} else {
						echo ' <a href="'.get_pagenum_link($i).'">'.$i.'</a> ';
					}
				}
			}
			next_posts_link($nxtlabel, $max_page);
			if (($paged+$half_pages_to_show) < ($max_page)) {
				echo ' <span class="fvpage">...</span> <a href="'.get_pagenum_link($max_page).'">Last &raquo;</a>';
			}
			echo "</strong>$after";
		}
	}
}
/* 结束分页 */

/* 判断是否为管理员 */
// Template tag: echoes semantic classes for a comment <li>
function comment_class() {
	global $comment, $post;

	$c = array($comment->comment_type, "c$comment_count");

	if ( $comment->user_id > 0 ) {
		$user = get_userdata($comment->user_id);

		$c[] = "byuser commentauthor-$user->user_login";

		if ( $comment->user_id === $post->post_author ) {
			$c[] = 'bypostauthor';
		}
	}
	echo join(' ', apply_filters('comment_class', $c));
}
/* 结束判断是否为管理员 */

/****************  get_gravatar With icon ********************/
function gr_all_icons($rating = false, $size = false, $default = false, $border = false, $threshhold = false) {
	global $wpdb;
	$comments = $wpdb->get_results("SELECT comment_author, comment_author_email, comment_author_url, user_id, COUNT(comment_ID) AS 'comment_total' FROM $wpdb->comments WHERE comment_approved = '1' AND user_id != 1 GROUP BY comment_author ORDER BY comment_total DESC");
	$cont = 1;

    foreach ($comments as $comment) {
        	$comment_author_email = "http://www.gravatar.com/avatar.php?gravatar_id=".md5($comment->comment_author_email);
			$comment_author = htmlspecialchars(stripslashes($comment->comment_author));
			$comment_author_url =stripslashes($comment->comment_author_url);
			$comment_total = (int) $comment->comment_total;
			
		    if($rating && $rating != '')
		    $comment_author_email .= "&amp;rating=".$rating;
	        if($size && $size != '')
     	    $comment_author_email .="&amp;size=".$size;
	        if($default && $default != '')
		    $comment_author_email .= "&amp;default=".urlencode($default);
	        if($border && $border != '')
		    $comment_author_email .= "&amp;border=".$border;
		    	
		    echo  "<a href=\"$comment_author_url\" title=\"$comment_author ($comment_total)\" target=\"_blank\"><img src=\"$comment_author_email\" class=\"get_gravatar\" alt=\"$comment_author\" /></a>";
		 
			if($comment_total <= $threshhold && $threshhold != -1) {
				return;
			}
    }
}


function gr_recent_comments($rating = false, $size = false, $default = false, $border = false, $no_comments = false) {

	global $wpdb;
	$request = "SELECT ID, comment_ID, comment_author_email, comment_content, comment_author FROM $wpdb->posts, $wpdb->comments WHERE $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish'";

    $request .= "AND comment_approved = '1' ORDER BY $wpdb->comments.comment_date DESC LIMIT 

$no_comments";
    $comments = $wpdb->get_results($request);
    foreach ($comments as $comment) {
       $comment_author_email = "http://www.gravatar.com/avatar.php?gravatar_id=".md5($comment->comment_author_email);
       $comment_author = stripslashes($comment->comment_author);
       $comment_content = strip_tags($comment->comment_content);
       $comment_content = stripslashes($comment_content);
       $comment_excerpt =substr($comment_content,0,32);
       $comment_excerpt = gr_utf8_trim($comment_excerpt);
       $permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID;
       
       	    if($rating && $rating != '')
		    $comment_author_email .= "&amp;rating=".$rating;
	        if($size && $size != '')
     	    $comment_author_email .="&amp;size=".$size;
	        if($default && $default != '')
		    $comment_author_email .= "&amp;default=".urlencode($default);
	        if($border && $border != '')
		    $comment_author_email .= "&amp;border=".$border;
       
       echo  "<a href=\"$permalink\" title=\"$comment_author Says: $comment_excerpt\"><img src=\"$comment_author_email\" class=\"get_gravatar\" alt=\"$comment_author\" /></a>";
       }
  	return;
}

function gr_utf8_trim($str) {

	$len = strlen($str);

	for ($i=strlen($str)-1; $i>=0; $i-=1){
		$hex .= ' '.ord($str[$i]);
		$ch = ord($str[$i]);
        if (($ch & 128)==0) return(substr($str,0,$i));
		if (($ch & 192)==192) return(substr($str,0,$i));
	}
	return($str.$hex);
}
/*  end get_gravatar With icon */

?>